{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d9361003",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[59.7, 10.0, 34.6]\n",
      "第0个聚落有12条线\n",
      "59.7 10.0 34.6\n",
      "中间投影,视图编号:0\n",
      "第1个聚落有12条线\n",
      "59.7 34.6 10.0\n",
      "下边投影,视图编号:1\n",
      "第2个聚落有12条线\n",
      "34.6 10.0 59.7\n",
      "右边投影,视图编号:2\n",
      "第3个聚落有12条线\n",
      "59.7 34.6 10.0\n",
      "上边投影,视图编号:3\n",
      "第4个聚落有12条线\n",
      "34.6 10.0 59.7\n",
      "左边投影,视图编号:4\n",
      "xy上下面线段集:24条线\n",
      "xz左右面线段集:24条线\n",
      "yz中间面线段集:12条线\n",
      "收集相关线段结束1:44\n",
      "收集相关线段结束:36\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "from 三维投影线段寻找 import get_adjusted_clusters,get_clusters,get_intersect_lines\n",
    "\n",
    "\n",
    "dxf_file_path = 'c.dxf'\n",
    "\n",
    "clusters=get_clusters(dxf_file_path)\n",
    "\n",
    "adjusted_clusters,yz中间面线段集,xy上下面线段集,xz左右面线段集  =get_adjusted_clusters(clusters)\n",
    "intersect_lines=get_intersect_lines(xz左右面线段集,yz中间面线段集,xy上下面线段集)\n",
    "intersect_lines1,intersect_lines2,intersect_lines3 = intersect_lines\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de7b15cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:31: DeprecationWarning: Call to deprecated function breplib_BuildCurves3d since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method breplib.BuildCurves3d\n",
      "  breplib_BuildCurves3d(wire)\n",
      "<frozen importlib._bootstrap>:241: DeprecationWarning: builtin type SwigPyPacked has no __module__ attribute\n",
      "<frozen importlib._bootstrap>:241: DeprecationWarning: builtin type SwigPyObject has no __module__ attribute\n",
      "<frozen importlib._bootstrap>:241: DeprecationWarning: builtin type swigvarlink has no __module__ attribute\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1916\\63881978.py:67: DeprecationWarning: Call to deprecated function Interface_Static_SetCVal since pythonocc-core 7.7.1. This function will be removed in a future release, please rather use the static method Interface_Static.SetCVal\n",
      "  Interface_Static_SetCVal(\"write.step.schema\", \"AP203\")  # 或者使用 AP214IS 或 AP242DIS\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finished.\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "from 三维投影线段寻找 import get_adjusted_clusters,get_clusters,get_intersect_lines\n",
    "\n",
    "\n",
    "dxf_file_path = 'c.dxf'\n",
    "\n",
    "clusters=get_clusters(dxf_file_path)\n",
    "\n",
    "adjusted_clusters,yz中间面线段集,xy上下面线段集,xz左右面线段集  =get_adjusted_clusters(clusters)\n",
    "intersect_lines=get_intersect_lines(xz左右面线段集,yz中间面线段集,xy上下面线段集)\n",
    "intersect_lines1,intersect_lines2,intersect_lines3 = intersect_lines\n",
    "\n",
    "import networkx as nx\n",
    "from OCC.Core.BRepBuilderAPI import (\n",
    "    BRepBuilderAPI_MakePolygon,\n",
    "    BRepBuilderAPI_MakeFace,\n",
    "    BRepBuilderAPI_MakeShell,\n",
    "    BRepBuilderAPI_MakeSolid\n",
    ")\n",
    "from OCC.Core.BRep import BRep_Builder\n",
    "from OCC.Core.TopoDS import TopoDS_Compound, TopoDS_Shell\n",
    "\n",
    "from OCC.Core.BRepLib import breplib_BuildCurves3d\n",
    "from OCC.Core.gp import gp_Pnt\n",
    "\n",
    "def get_closed_subgraphs_in_open_faces(intersect_lines):\n",
    "    all_polygons = []\n",
    "    G = nx.Graph()\n",
    "    G.add_edges_from(intersect_lines)\n",
    "    cycles = list(nx.cycle_basis(G))\n",
    "    for cycle in cycles:\n",
    "        if cycle[0] != cycle[-1]:\n",
    "            cycle.append(cycle[0])\n",
    "        all_polygons.append(cycle)\n",
    "    return all_polygons\n",
    "\n",
    "def create_face_from_polygon(points):\n",
    "    make_polygon = BRepBuilderAPI_MakePolygon()\n",
    "    for pt in points:\n",
    "        make_polygon.Add(gp_Pnt(*pt))\n",
    "    make_polygon.Close()\n",
    "    wire = make_polygon.Wire()\n",
    "    breplib_BuildCurves3d(wire)\n",
    "    face = BRepBuilderAPI_MakeFace(wire).Face()\n",
    "    return face\n",
    "\n",
    "def create_shell_and_solid(polygons):\n",
    "    \"\"\"\n",
    "    输入：polygons 是所有多边形点集 [[(x, y, z), ...], ...]\n",
    "    输出：TopoDS_Solid\n",
    "    \"\"\"\n",
    "    builder = BRep_Builder()\n",
    "    shell = TopoDS_Shell()\n",
    "    builder.MakeShell(shell)\n",
    "    for polygon in polygons:\n",
    "        face = create_face_from_polygon(polygon)\n",
    "        builder.Add(shell, face)\n",
    "    solid_builder = BRepBuilderAPI_MakeSolid(shell)\n",
    "    solid = solid_builder.Solid()\n",
    "    return solid\n",
    "\n",
    "\n",
    "\n",
    "# 生成所有多边形\n",
    "polygons1 = get_closed_subgraphs_in_open_faces(intersect_lines1)\n",
    "polygons2 = get_closed_subgraphs_in_open_faces(intersect_lines2)\n",
    "polygons3 = get_closed_subgraphs_in_open_faces(intersect_lines3)\n",
    "all_polygons = polygons1 + polygons2 + polygons3\n",
    "\n",
    "# 构建实体\n",
    "solid = create_shell_and_solid(all_polygons)\n",
    "\n",
    "print(\"finished.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
